<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>任务详情</title>
    <!-- 引入jquery -->
    <script src="../../node_modules/jquery/dist/jquery.min.js"></script>
    <!-- 引入layui -->
    <link href="../../node_modules/layui/dist/css/layui.css" rel="stylesheet">
    <script src="../../node_modules/layui/dist/layui.js"></script>
    <style>
        body {
            background-color: #f5f5f5;
            padding: 20px;
        }

        .detail-container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            height: 100%;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .page-title {
            margin-bottom: 10px;
            padding-bottom: 5px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: flex-start; /* 改为左对齐 */
            align-items: center;
            position: relative;
            gap: 20px; /* 添加间距 */
        }

        .page-title h2 {
            margin: 0;
            color: #333;
            font-size: 24px;
            font-weight: 500;
        }

        .layui-form-label {
            width: 100px;
        }

        .layui-input-block {
            margin-left: 130px;
        }

        .layui-textarea {
            min-height: 100px;
            font-family: Consolas, Monaco, 'Courier New', monospace;
        }

        /* 类别标签样式 */
        .category-tags {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
            min-height: 24px;
            margin-left: 10px;
            flex: 1;
        }

        .category-tag {
            display: inline-flex;
            align-items: center;
            padding: 2px 8px;
            background-color: #1E9FFF;
            color: white;
            border-radius: 12px;
            font-size: 12px;
            box-shadow: 0 1px 2px rgba(30, 159, 255, 0.2);
            transition: all 0.2s ease;
            height: 24px;
        }

        .category-tag:nth-child(3n+1) {
            background-color: #1E9FFF;
        }

        .category-tag:nth-child(3n+2) {
            background-color: #009688;
        }

        .category-tag:nth-child(3n+3) {
            background-color: #FFB800;
        }

        .category-tag .close-btn {
            margin-left: 4px;
            cursor: pointer;
            font-size: 14px;
            opacity: 0.8;
            font-weight: bold;
            width: 14px;
            height: 14px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.2);
            margin-right: -2px;
        }

        .category-tag .close-btn:hover {
            opacity: 1;
            background-color: rgba(255, 255, 255, 0.3);
        }

        .category-select-group {
            display: flex;
            gap: 10px;
            align-items: center;
            flex: 1;
        }

        .category-select-group select {
            width: 150px;
            padding: 8px;
            border: 1px solid #e6e6e6;
            border-radius: 2px;
            flex-shrink: 0;
        }

        #taskDefinition {
            height: 200px;
        }

        .back-button {
            position: absolute;
            top: 20px;
            right: 20px;
            padding: 8px 16px;
            background-color: #2c3e50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: all 0.3s ease;
        }

        .back-button:hover {
            background-color: #34495e;
            transform: translateX(-3px);
        }

        .back-button::before {
            content: '←';
            font-size: 16px;
        }

        /* 修改确定按钮样式 */
        .submit-button {
            padding: 6px 20px;
            background-color: #1E9FFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s ease;
            margin-left: auto; /* 将按钮推到最右侧 */
            position: relative;
        }

        .submit-button:hover {
            background-color: #0d8aee;
            box-shadow: 0 2px 5px rgba(30, 159, 255, 0.2);
        }

        /* 移除原有的按钮容器样式 */
        .form-buttons {
            display: none;
        }
    </style>
</head>

<body>
<div class="detail-container">
    <button class="back-button" id="btn-back">返回</button>
    <div class="page-title">
        <h2>任务详情</h2>
        <button type="submit" class="submit-button" lay-submit lay-filter="saveDetail" id="btn-save">保存</button>
    </div>

    <form class="layui-form" lay-filter="detailForm">
        <div class="layui-form-item">
            <label class="layui-form-label">任务名</label>
            <div class="layui-input-block">
                <input type="text" name="taskName" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类别</label>
            <div class="layui-input-block">
                <div class="category-select-group">
                    <select name="category" lay-filter="category" id="category-selector">
                        <option value="">选择类别(至多三项)</option>

                    </select>
                    <div class="category-tags" id="categoryTags"></div>
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">网址</label>
            <div class="layui-input-block">
                <textarea name="urls" class="layui-textarea" style="resize: none; white-space: pre-wrap;"
                          readonly></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">任务描述</label>
            <div class="layui-input-block">
                <textarea name="description" class="layui-textarea" style="resize: none;"></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">任务定义</label>
            <div class="layui-input-block">
                <textarea id="taskDefinition" name="taskDefinition" class="layui-textarea"
                          style="resize: none; white-space: pre-wrap;" readonly></textarea>
            </div>
        </div>
    </form>
</div>

<script>
    const form = layui.form;
    const selectedCategories = new Set();

    // 监听类别选择
    form.on('select(category)', function (data) {
        const value = data.value;
        if (!value) return;

        // 检查是否已达到最大选择数量
        if (selectedCategories.size >= 3) {
            layer.msg('最多只能选择3个类别');
            // 重置选择框
            data.elem.selectedIndex = 0;
            form.render('select');
            return;
        }

        // 检查是否已经选择过该类别
        if (selectedCategories.has(value)) {
            layer.msg('该类别已经选择过了');
            // 重置选择框
            data.elem.selectedIndex = 0;
            form.render('select');
            return;
        }

        // 添加新的类别标签
        addCategoryTag(value, $(data.elem).find("option:selected").text());

        // 重置选择框
        data.elem.selectedIndex = 0;
        form.render('select');
    });

    // 添加类别标签
    function addCategoryTag(value, text) {
        selectedCategories.add(value);

        const tag = document.createElement('div');
        tag.className = 'category-tag';
        tag.innerHTML = text + '<span class="close-btn" data-value="' + value + '">×</span>';

        // 添加删除按钮事件
        tag.querySelector('.close-btn').addEventListener('click', function () {
            selectedCategories.delete(value);
            tag.remove();
        });

        document.getElementById('categoryTags').appendChild(tag);
    }


</script>
<script src="crawler_detail_render.js"></script>
</body>
</html>